{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "467f13fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f8184ebd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "[4 5]\n",
      "[0 2 4 6 8]\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# 1.一维数组的索引和切片\n",
    "a1 = np.arange(10)\n",
    "# 1.1进行索引操作\n",
    "print(a1[4])\n",
    "# 1.2进行切片操作\n",
    "print(a1[4:6])\n",
    "# 1.3使用步长\n",
    "print(a1[::2])\n",
    "# 1.4使用负数作为索引\n",
    "print(a1[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8afd6815",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 8 1 2 2 7]\n",
      " [7 3 3 6 7 7]\n",
      " [9 6 3 3 0 8]\n",
      " [2 1 8 2 5 1]]\n"
     ]
    }
   ],
   "source": [
    "# 2.多维数组\n",
    "# 也是通过中括号来索引和切片，在中括号中，使用逗号进行分割，逗号前面的是行，逗号后面的是列\n",
    "# 如果多维数组中只有一个值，那么这个值就是行\n",
    "a2 = np.random.randint(0,10,size=(4,6))\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5ea4ef66",
   "metadata": {},
   "outputs": [],
   "source": [
    "# a2[0]\n",
    "# a2[1:3]\n",
    "# a2[[0,2,3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "989c12ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 2],\n",
       "       [3, 6],\n",
       "       [6, 3],\n",
       "       [1, 2]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a2[2,1]\n",
    "# print(a2[[1,2],[4,5]])\n",
    "# print(a2[1:3,4:6])\n",
    "a2[:,[1,3]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f111c08",
   "metadata": {},
   "source": [
    "# 索引和切片总结\n",
    "1.如果数组是一维的，那么索引和切片就是和python里的列表是一样的\n",
    "2.如果数组是多维的（这里以二维为例），那么在中括号中，给两个值，两个值是通过逗号隔开的，逗号前面的是行，逗号后面的是列。如果中括号中只有一个值，那么就是代表行。\n",
    "3.如果是多维数组（这里以二维为例），那么行的部分和列的部分，都是遵循一维数组的方式，可以使用整形，切片，还可以使用中括号的形式来代表不连续的。比如a[[1,2],[3,4]],那么返回的就是(1,3),(2,4)的两个值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "01181890",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.arange(0,24).reshape((4,6))\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e826b04c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ True  True  True  True  True  True]\n",
      " [ True  True  True  True False False]\n",
      " [False False False False False False]\n",
      " [False False False False False False]]\n"
     ]
    }
   ],
   "source": [
    "print(a1<10)\n",
    "# 会返回一个新的数组，这个数组中的值全部都是bool类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b5f9d88c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1[a1 < 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d2243a7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,\n",
       "       23])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1[(a1 < 5)|(a1 > 10)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cc877ec",
   "metadata": {},
   "source": [
    "# 布尔索引\n",
    "布尔索引是通过相同数组上的True还是False来进行提取的。提取的条件有多个，那么如果有多个，可以使用&来代表且，用|来代表或，如果有多个条件，那么每个条件都要使用圆括号括起来。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eab18fff",
   "metadata": {},
   "source": [
    "# 值的替换"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93585a7f",
   "metadata": {},
   "source": [
    "利用索引做一些值的替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e2cffc47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 0, 9, 3, 0],\n",
       "       [3, 8, 7, 2, 7],\n",
       "       [0, 3, 2, 1, 5]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3 = np.random.randint(0,10,size=(3,5))\n",
    "a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5ccc000a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 0, 9, 3, 0],\n",
       "       [1, 2, 3, 4, 5],\n",
       "       [0, 3, 2, 1, 5]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3[1] = np.array([1,2,3,4,5])\n",
    "a3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "544d9bda",
   "metadata": {},
   "source": [
    "也可以利用条件索引来实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "87d0e494",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 1, 9, 3, 1],\n",
       "       [1, 1, 3, 4, 5],\n",
       "       [1, 3, 1, 1, 5]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3[a3 < 3] = 1\n",
    "a3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95d3b545",
   "metadata": {},
   "source": [
    "还可以使用where函数来实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5de8a198",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 1, 0, 0],\n",
       "       [0, 0, 0, 0, 1],\n",
       "       [0, 0, 0, 0, 1]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = np.where(a3<5,0,1)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec57d780",
   "metadata": {},
   "source": [
    "# 值的替换总结\n",
    "1.可以使用索引或者切片来替换\n",
    "2.使用条件索引来替换\n",
    "3.使用where函数来实现"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
